package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.system.domain.ByBs;
import com.ruoyi.system.mapper.ByBsMapper;
import com.ruoyi.system.mapper.DateUtilSqlMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.ByBsTimeMapper;
import com.ruoyi.system.domain.ByBsTime;
import com.ruoyi.system.service.IByBsTimeService;

/**
 * 帮手时间安排Service业务层处理
 *
 * @author ruoyi
 * @date 2023-05-27
 */
@Service
public class ByBsTimeServiceImpl implements IByBsTimeService {
    @Autowired
    private ByBsTimeMapper byBsTimeMapper;

    @Autowired
    private DateUtilSqlMapper dateUtilSqlMapper;

    @Autowired
    private ByBsMapper bsMapper;


   /* @Override
    public String getDateAddDay(int days) {
        return dateUtilSqlMapper.getDateAddDay(days);
    }*/

    @Override
    public List<JSONObject> getBsDateMoreNow(String id) {
        return byBsTimeMapper.getBsDateMoreNow(id);
    }

    @Override
    public List<JSONObject> showBsTimeDataForDate(JSONObject json) {
        String userid = json.getString("userid");
        ByBs bsEntity = bsMapper.queryByWxid(userid);
        String bsid = bsEntity.getId()+"";
        String date = json.getString("date");
        System.out.println(date);
        //查询当前日期帮手是否设置过
        List<ByBsTime> list = byBsTimeMapper.getBsTimeForByIdAndDate(bsid, date);
        if(list.size() > 0){
            ByBsTime entity = list.get(0);
            return getBsTimeFormatList(entity);
        }

        return null;
    }

    public List<JSONObject> getBsTimeFormatList(ByBsTime entity){
        List<JSONObject> list = new ArrayList<>();


        JSONObject json8 = new JSONObject();
        json8.put("time","8:00");
        json8.put("status",entity.getEight());
        list.add(json8);

        JSONObject json9 = new JSONObject();
        json9.put("time","9:00");
        json9.put("status",entity.getNine());
        list.add(json9);

        JSONObject json10 = new JSONObject();
        json10.put("time","10:00");
        json10.put("status",entity.getTen());
        list.add(json10);


        JSONObject json11 = new JSONObject();
        json11.put("time","11:00");
        json11.put("status",entity.getEleven());
        list.add(json11);


        JSONObject json12 = new JSONObject();
        json12.put("time","12:00");
        json12.put("status",entity.getTwelve());
        list.add(json12);


        JSONObject json13 = new JSONObject();
        json13.put("time","13:00");
        json13.put("status",entity.getThirteen());
        list.add(json13);

        JSONObject json14 = new JSONObject();
        json14.put("time","14:00");
        json14.put("status",entity.getFourteen());
        list.add(json14);

        JSONObject json15 = new JSONObject();
        json15.put("time","15:00");
        json15.put("status",entity.getFifteen());
        list.add(json15);

        JSONObject json16 = new JSONObject();
        json16.put("time","16:00");
        json16.put("status",entity.getSixteen());
        list.add(json16);


        JSONObject json17 = new JSONObject();
        json17.put("time","17:00");
        json17.put("status",entity.getSeventeen());
        list.add(json17);

        JSONObject json18 = new JSONObject();
        json18.put("time","18:00");
        json18.put("status",entity.getEighteen());
        list.add(json18);

        //课程时间安排增加18：30
        JSONObject json19 = new JSONObject();
        json19.put("time","18:30");
        json19.put("status",entity.getHalfpasteighteen());
        list.add(json19);
        return list;
    }


    @Override
    public void updateBsTimestatus(JSONObject json) {
        String userid = json.getString("userid");
        ByBs bsEntity = bsMapper.queryByWxid(userid);
        String bsid = bsEntity.getId()+"";
        String date = json.getString("date");
        String status = json.getString("status");
        String time = json.getString("time");


        //查询当前日期帮手是否设置过
        List<ByBsTime> list = byBsTimeMapper.getBsTimeForByIdAndDate(bsid, date);
        if(list.size() > 0){
            ByBsTime entity = list.get(0);
            entity = newSetBsTime(entity,time,status);
            byBsTimeMapper.updateByBsTime(entity);
        }

    }


    private ByBsTime newSetBsTime(ByBsTime entity,String time,String status){
        if(status.equals("0")){
            status = "1";
        }else if(status.equals("1")){
            status = "0";
        }
        if(time.trim().equals("8:00")){
            entity.setEight(status);
        }
        if(time.trim().equals("9:00")){
            entity.setNine(status);
        }
        if(time.trim().equals("10:00")){
            entity.setTen(status);
        }
        if(time.trim().equals("11:00")){
            entity.setEleven(status);
        }
        if(time.trim().equals("12:00")){
            entity.setTwelve(status);
        }
        if(time.trim().equals("13:00")){
            entity.setThirteen(status);
        }
        if(time.trim().equals("14:00")){
            entity.setFourteen(status);
        }
        if(time.trim().equals("15:00")){
            entity.setFifteen(status);
        }
        if(time.trim().equals("16:00")){
            entity.setSixteen(status);
        }
        if(time.trim().equals("17:00")){
            entity.setSeventeen(status);
        }
        if(time.trim().equals("18:00")){
            entity.setEighteen(status);
        }
        if(time.trim().equals("18:30")){
            entity.setHalfpasteighteen(status);
        }

        return entity;
    }


    @Override
    public void bsInDateData(String userid) {
        // 通过微信id获取帮手id
        ByBs bsEntity = bsMapper.queryByWxid(userid);
        String nowDate = dateUtilSqlMapper.getNowDate();
        String nextDate = dateUtilSqlMapper.getNextDate();
        String bsid = bsEntity.getId()+"";
        ByBsTime bsTime = null;

        //查询当前日期帮手是否设置过
        List<ByBsTime> nowlist = byBsTimeMapper.getBsTimeForByIdAndDate(bsid, nowDate);
        if(nowlist.size() == 0){ // 未绑定
            //进行数据插入
            bsTime = new ByBsTime();
            bsTime.setBsId(bsid);
            bsTime.setDate(nowDate);
            bsTime.setAddtime(dateUtilSqlMapper.getNowDateTime());
            bsTime.setUptime(dateUtilSqlMapper.getNowDateTime());

            byBsTimeMapper.insertByBsTime(bsTime);
        }


        //查询明天日期帮手是否设置过
        List<ByBsTime> nextDatelist = byBsTimeMapper.getBsTimeForByIdAndDate(bsid, nextDate);
        if(nextDatelist.size() == 0){ // 未绑定
            //进行数据插入
            bsTime = new ByBsTime();
            bsTime.setBsId(bsid);
            bsTime.setDate(nextDate);
            bsTime.setAddtime(dateUtilSqlMapper.getNowDateTime());
            bsTime.setUptime(dateUtilSqlMapper.getNowDateTime());

            byBsTimeMapper.insertByBsTime(bsTime);
        }

        String threeDate = dateUtilSqlMapper.getDateAddDay(2);
        nextDatelist = byBsTimeMapper.getBsTimeForByIdAndDate(bsid, threeDate);
        if(nextDatelist.size() == 0){ // 未绑定
            //进行数据插入
            bsTime = new ByBsTime();
            bsTime.setBsId(bsid);
            bsTime.setDate(threeDate);
            bsTime.setAddtime(dateUtilSqlMapper.getNowDateTime());
            bsTime.setUptime(dateUtilSqlMapper.getNowDateTime());

            byBsTimeMapper.insertByBsTime(bsTime);
        }

        String fourDate = dateUtilSqlMapper.getDateAddDay(3);
        nextDatelist = byBsTimeMapper.getBsTimeForByIdAndDate(bsid, fourDate);
        if(nextDatelist.size() == 0){ // 未绑定
            //进行数据插入
            bsTime = new ByBsTime();
            bsTime.setBsId(bsid);
            bsTime.setDate(fourDate);
            bsTime.setAddtime(dateUtilSqlMapper.getNowDateTime());
            bsTime.setUptime(dateUtilSqlMapper.getNowDateTime());

            byBsTimeMapper.insertByBsTime(bsTime);
        }

        String fiveDate = dateUtilSqlMapper.getDateAddDay(4);
        nextDatelist = byBsTimeMapper.getBsTimeForByIdAndDate(bsid, fiveDate);
        if(nextDatelist.size() == 0){ // 未绑定
            //进行数据插入
            bsTime = new ByBsTime();
            bsTime.setBsId(bsid);
            bsTime.setDate(fiveDate);
            bsTime.setAddtime(dateUtilSqlMapper.getNowDateTime());
            bsTime.setUptime(dateUtilSqlMapper.getNowDateTime());

            byBsTimeMapper.insertByBsTime(bsTime);
        }

        String sixeDate = dateUtilSqlMapper.getDateAddDay(5);
        nextDatelist = byBsTimeMapper.getBsTimeForByIdAndDate(bsid, sixeDate);
        if(nextDatelist.size() == 0){ // 未绑定
            //进行数据插入
            bsTime = new ByBsTime();
            bsTime.setBsId(bsid);
            bsTime.setDate(sixeDate);
            bsTime.setAddtime(dateUtilSqlMapper.getNowDateTime());
            bsTime.setUptime(dateUtilSqlMapper.getNowDateTime());

            byBsTimeMapper.insertByBsTime(bsTime);
        }

        String sevenDate = dateUtilSqlMapper.getDateAddDay(6);
        nextDatelist = byBsTimeMapper.getBsTimeForByIdAndDate(bsid, sevenDate);
        if(nextDatelist.size() == 0){ // 未绑定
            //进行数据插入
            bsTime = new ByBsTime();
            bsTime.setBsId(bsid);
            bsTime.setDate(sevenDate);
            bsTime.setAddtime(dateUtilSqlMapper.getNowDateTime());
            bsTime.setUptime(dateUtilSqlMapper.getNowDateTime());

            byBsTimeMapper.insertByBsTime(bsTime);
        }
    }

    @Override
    public JSONObject getDateAndNextDate() {
        JSONObject json = new JSONObject();
        String nowDate = dateUtilSqlMapper.getNowDate();
        String nextDate = dateUtilSqlMapper.getNextDate();

        String threeDate = dateUtilSqlMapper.getDateAddDay(2);
        String fourDate = dateUtilSqlMapper.getDateAddDay(3);
        String fiveDate = dateUtilSqlMapper.getDateAddDay(4);
        String sixeDate = dateUtilSqlMapper.getDateAddDay(5);
        String sevenDate = dateUtilSqlMapper.getDateAddDay(6);

        json.put("nowDate",nowDate);
        json.put("nextDate",nextDate);
        json.put("threeDate",threeDate);
        json.put("fourDate",fourDate);
        json.put("fiveDate",fiveDate);
        json.put("sixeDate",sixeDate);
        json.put("sevenDate",sevenDate);
        return json;
    }

    /**
     * 查询帮手时间安排
     *
     * @param id 帮手时间安排主键
     * @return 帮手时间安排
     */
    @Override
    public ByBsTime selectByBsTimeById(Long id)
    {
        return byBsTimeMapper.selectByBsTimeById(id);
    }

    /**
     * 查询帮手时间安排列表
     *
     * @param byBsTime 帮手时间安排
     * @return 帮手时间安排
     */
    @Override
    public List<ByBsTime> selectByBsTimeList(ByBsTime byBsTime)
    {
        return byBsTimeMapper.selectByBsTimeList(byBsTime);
    }

    /**
     * 新增帮手时间安排
     *
     * @param byBsTime 帮手时间安排
     * @return 结果
     */
    @Override
    public int insertByBsTime(ByBsTime byBsTime)
    {
        return byBsTimeMapper.insertByBsTime(byBsTime);
    }

    /**
     * 修改帮手时间安排
     *
     * @param byBsTime 帮手时间安排
     * @return 结果
     */
    @Override
    public int updateByBsTime(ByBsTime byBsTime)
    {
        return byBsTimeMapper.updateByBsTime(byBsTime);
    }

    /**
     * 批量删除帮手时间安排
     *
     * @param ids 需要删除的帮手时间安排主键
     * @return 结果
     */
    @Override
    public int deleteByBsTimeByIds(Long[] ids)
    {
        return byBsTimeMapper.deleteByBsTimeByIds(ids);
    }

    /**
     * 删除帮手时间安排信息
     *
     * @param id 帮手时间安排主键
     * @return 结果
     */
    @Override
    public int deleteByBsTimeById(Long id)
    {
        return byBsTimeMapper.deleteByBsTimeById(id);
    }

    @Override
    public List<String> selectByBsTimeListNow(String id) {
        List<ByBsTime> timeList = byBsTimeMapper.selectByBsTimeListNow(id);
        List<String> list = new ArrayList<>();
        for(ByBsTime time: timeList){
            if(time.getEight().equals("1")){
                list.add(time.getDate() + " 08:00");
            }
            if(time.getNine().equals("1")){
                list.add(time.getDate() + " 09:00");
            }
            if(time.getTen().equals("1")){
                list.add(time.getDate() + " 10:00");
            }
            if(time.getEleven().equals("1")){
                list.add(time.getDate() + " 11:00");
            }
            if(time.getTwelve().equals("1")){
                list.add(time.getDate() + " 12:00");
            }
            if(time.getThirteen().equals("1")){
                list.add(time.getDate() + " 13:00");
            }
            if(time.getFourteen().equals("1")){
                list.add(time.getDate() + " 14:00");
            }
            if(time.getFifteen().equals("1")){
                list.add(time.getDate() + " 15:00");
            }
            if(time.getSixteen().equals("1")){
                list.add(time.getDate() + " 16:00");
            }
            if(time.getSeventeen().equals("1")){
                list.add(time.getDate() + " 17:00");
            }
            if(time.getEighteen().equals("1")){
                list.add(time.getDate() + " 18:00");
            }
            if(time.getHalfpasteighteen().equals("1")){
                list.add(time.getDate() + " 18:30");
            }
        }
        return list;
    }
}
